package agent

import (
	"cloudops/src/config"
	"context"
	"github.com/shimingyah/pool"
	"go.uber.org/zap"
	"time"
)

// agent 内部的公共传递介质
type Client struct {
	Pool pool.Pool
	Sc   *config.AgentConfig
}

// 生成公共原数据方法
func (c *Client) GenTwContext() (context.Context, context.CancelFunc) {
	return context.WithTimeout(context.Background(), time.Duration(c.Sc.RpcCallTimeOutSeconds)*time.Second)
}

// 创建grpc连接池
func NewGrpcClient(sc *config.AgentConfig) *Client {
	client := &Client{}
	p, err := pool.New(sc.RpcServerAddr, pool.DefaultOptions)
	if err != nil {
		sc.Logger.Fatal("创建grpc连接池失败", zap.Error(err))
	}
	client.Pool = p
	client.Sc = sc
	return client
}
